package com.xh.bussiness.thirdapi.xinzhou.controller;

import com.xh.bussiness.permission.model.User;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzGroupRequest;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzScheduleRequest;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzStudentInfoRequest;
import com.xh.bussiness.thirdapi.xinzhou.model.XzTeacherInfo;
import com.xh.bussiness.thirdapi.xinzhou.service.IXinzhouTmsService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/xzTMS")
public class XinzhouTMSController {

    @Autowired
    private IXinzhouTmsService xinzhouTmsService;

    @RequestMapping("/queryGrade")
    public void queryGrade(HttpSession session){
        Object user = session.getAttribute("user" );
        XzGroupRequest xzGroupRequest = new XzGroupRequest();
        xzGroupRequest.setTeacherId("aaaaa");
        xinzhouTmsService.queryGroupList(xzGroupRequest);
    }

    @RequestMapping("/queryTeacherInfo")
    public AjaxResult queryTeacherInfo(String mobile){
       return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", xinzhouTmsService.getTeacherByMobile(mobile));
    }

    @RequestMapping("/querySchedule")
    public AjaxResult querySchedule(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        String mobile = user.getMobile();
        List<XzTeacherInfo> teacherByMobiles = xinzhouTmsService.getTeacherByMobile(mobile);
        if(CollectionUtils.isEmpty(teacherByMobiles)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"用户手机号不匹配，请检查后再重试!");
        }
        XzTeacherInfo xzTeacherInfo = teacherByMobiles.get(0);
        XzScheduleRequest  scheduleRequest = new XzScheduleRequest();
        scheduleRequest.setTeacherid(xzTeacherInfo.getUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", xinzhouTmsService.querySchedule(scheduleRequest));
    }

    @RequestMapping("/synStudents")
    public AjaxResult synStudents(XzStudentInfoRequest request){
        try {
            request.setSearchType(1);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", xinzhouTmsService.queryStudents(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"同步失败，请重试！");
        }
    }

    @RequestMapping("/queryStudentContract")
    public AjaxResult queryStudentContract(XzStudentInfoRequest request){
        try {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", xinzhouTmsService.queryStudentContract(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"同步失败，请重试！");
        }
    }

    @RequestMapping("/synSchools")
    public AjaxResult synSchools(){
        try {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", xinzhouTmsService.synSchools());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"同步失败，请重试！");
        }
    }
}
